package com.foreveross.fetl.queue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.foreveross.fetl.application.ICsairTaobaoApplication;
import com.foreveross.fetl.queue.util.LogMessageUtils;
import com.foreveross.fetl.util.DateUtils;
import com.foreveross.fetl.util.StringUtils;

@Component
public class CsairTaobaoPreprocess {
	
private static Logger log = Logger.getLogger("csairTaobao");
	
	@Inject ICsairTaobaoApplication csairTaobaoApplication;
	
	private int tasksNumber;//任务数 多少条rowkey
	
	private int failuresNumber;//失败数
	
	private int insertNumber; //插入条数 
	
	private long excuteStartTime;
	
	
	private String taskTime=null;
	
	public void  rollBackNumber(){
		tasksNumber=0;
		failuresNumber=0;
		insertNumber=0;
		excuteStartTime = System.currentTimeMillis();
		LogMessageUtils.create("南航淘宝价差", this.getClass().getSimpleName());
		
	}
	public void execute(){
		final Calendar c = Calendar.getInstance();
		c.add(Calendar.HOUR_OF_DAY, -1);
		rollBackNumber();
		taskTime=DateUtils.format(c.getTime(), "yyyy-MM-dd HH");
		try {
			process(c.getTime(), new Date());
		} catch (Exception e) {
			LogMessageUtils.build("南航淘宝价差", this.getClass().getSimpleName(), 3, StringUtils.getStackTrace(e), failuresNumber, tasksNumber, 
					insertNumber, taskTime, excuteStartTime);
		}
		LogMessageUtils.build("南航淘宝价差", this.getClass().getSimpleName(), 2, null, 
				failuresNumber, tasksNumber, insertNumber, taskTime, excuteStartTime);		
	}
	
	public void manualExecute(final String start,String time){
		this.rollBackNumber();
		try {
		List<String> timeList = Arrays.asList(time.split(";"));
		Calendar c = Calendar.getInstance();
		taskTime = start + " " +timeList.get(0)+ "~" +timeList.get(timeList.size()-1);
		String str =start;
		str += timeList.get(0);
		Date startTime = DateUtils.parse(str, "yyyyMMddHH");
		Date endTime=DateUtils.parse(start+timeList.get(timeList.size()-1), "yyyyMMddHH");
		c.setTime(endTime);
		c.add(Calendar.HOUR_OF_DAY, +1);
		this.process(startTime, c.getTime());
	/*	for (String s : timeList) {
			String str =start;
			str += s;
			Date startTime = DateUtils.parse(str, "yyyyMMddHH");
			c.setTime(startTime);
			c.add(Calendar.HOUR_OF_DAY, +1);
			Date endTime = c.getTime();
			this.process(startTime, endTime);
			}*/
		LogMessageUtils.build("南航淘宝价差", this.getClass().getSimpleName(), 2, null, 
				failuresNumber, tasksNumber, insertNumber, taskTime, excuteStartTime);
		} catch (Exception e) {
			LogMessageUtils.build("南航淘宝价差", this.getClass().getSimpleName(), 3, StringUtils.getStackTrace(e), failuresNumber, tasksNumber, 
					insertNumber, taskTime, excuteStartTime);
		}
	}
	

	private void process(Date start, Date end) {
		log.info("AirChinaDomesticPreprocess start:"
				+ DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
		List<String> listLine =this.getAirLine();
		List<String> airCompanys = this.getAirCompanys();
		List<String> compareChanels= this.getCompareChannels();
		for(String str :listLine){
			String[] lines=str.split("-");
			Calendar cFlightDate = Calendar.getInstance();
			cFlightDate.setTime(start);
	//		int count = cFlightDate.get(Calendar.DAY_OF_WEEK);
			tasksNumber++;
			try {
				cFlightDate.add(Calendar.DAY_OF_MONTH, 2); //第二天
//				 if(1==count){
//					//周日 取当天
//				}else{
//					// 其它 
//					cFlightDate.add(Calendar.DAY_OF_MONTH, 8-count);
//				}
					insertNumber += csairTaobaoApplication.createReport(
							airCompanys, compareChanels, lines[0], lines[1],
							start,end,cFlightDate.getTime());
			} catch (Exception e) {
				log.error(StringUtils.getStackTrace(e));
				failuresNumber++;
			}
		}
		
	}
	

	/**
	 * 对比的航空公司
	 * @return
	 */
	private List<String> getAirCompanys(){
		List<String> airCompanys = new ArrayList<String>();
		airCompanys.add("20005"); //南航
		return airCompanys;
	}
	
	/**
	 * 对比的渠道
	 * @return
	 */
	private List<String> getCompareChannels(){
		List<String> chanels=new ArrayList<String>();
		chanels.add("00001"); //淘宝
		chanels.add("00002");//去哪儿
		return chanels;
	}
	/**
	 * 航线
	 * @return
	 */
	private List<String> getAirLine(){
		List<String> listLine = new ArrayList<String>();
		//旧的100条
		/*String[] lines = { "PEK-URC", "PEK-CAN", "URC-PEK", "KWE-PEK",
				"PEK-SZX", "SYX-PEK", "CAN-PEK", "YNT-CTU", "PEK-SYX",
				"HRB-SYX", "CAN-URC", "CAN-SYX", "SZX-PEK", "CTU-CAN",
				"CAN-SHA", "SHA-URC", "HRB-CAN", "CAN-DLC", "SYX-CAN",
				"HAK-CAN", "CAN-CGO", "NNG-PEK", "SYX-HRB", "CAN-HRB",
				"WUH-URC", "HRB-PVG", "CKG-CAN", "PEK-KWE", "URC-CAN",
				"URC-SHA", "CGO-URC", "HAK-PEK", "CAN-CTU", "HGH-CAN",
				"CSX-URC", "CAN-HAK", "DLC-CAN", "XMN-URC", "CAN-HGH",
				"CGO-SYX", "PEK-NNG", "PEK-HAK", "CSX-PEK", "CAN-XIY",
				"CAN-CGQ", "SHE-CAN", "CAN-KMG", "KWE-CAN", "CGQ-SZX",
				"XIY-CAN", "CAN-CKG", "PEK-ZUH", "CKG-PEK", "CGO-HAK",
				"WUH-KMG", "CGQ-PEK", "HRB-PEK", "URC-CGO", "DLC-PVG",
				"WUH-XMN", "SHE-PVG", "KMG-CAN", "HAK-CGO", "CGQ-CAN",
				"WUH-PEK", "CGO-CAN", "PEK-CTU", "DLC-PEK", "CTU-SZX",
				"SHE-SYX", "SHA-SZX", "WUH-CTU", "HGH-SZX", "CAN-YIW",
				"WUH-CAN", "YIW-CAN", "SYX-SHE", "CAN-SHE", "URC-SYX",
				"NNG-CAN", "NKG-CAN", "SYX-PVG", "KMG-PEK", "PVG-SHE",
				"SHA-CAN", "CKG-SZX", "HAK-URC", "WUH-SZX", "SZX-HRB",
				"CTU-URC", "SZX-HGH", "CAN-KWE", "PEK-HRB", "ZUH-PEK",
				"WUH-HAK", "CGQ-PVG", "PEK-CKG", "WUH-PVG", "URC-CTU",
				"CAN-NNG",

		};*/
		//新的100条
		String[] lines = { "PEK-CAN", "CAN-PEK", "SYX-PEK", "CTU-CAN",
				"PEK-SZX", "CTU-PEK", "PEK-URC", "CAN-SHA", "CAN-HGH",
				"CAN-URC", "PEK-SYX", "SZX-PEK", "CAN-CTU", "SHA-CAN",
				"CTU-SZX", "HGH-CAN", "PEK-KWE", "URC-PEK", "LJG-KMG",
				"SHA-URC", "CAN-WNZ", "SYX-PVG", "CAN-CKG", "CAN-HAK",
				"CTU-CGO", "KWE-CAN", "HAK-CAN", "CAN-SYX", "CAN-KMG",
				"CAN-YIW", "HAK-PEK", "CKG-CAN", "PVG-SYX", "SYX-CAN",
				"URC-CAN", "SHA-SZX", "NKG-CAN", "CAN-TSN", "CTU-URC",
				"KMG-CAN", "SYX-HRB", "SZX-CTU", "CAN-DLC", "HRB-CAN",
				"PEK-NNG", "SYX-CKG", "SZX-SHA", "DLC-PVG", "PEK-HAK",
				"CAN-NKG", "KWE-PEK", "CTU-WUH", "YIW-CAN", "SHE-CAN",
				"URC-SHA", "CKG-KMG", "SHE-PVG", "CAN-SHE", "HGH-SZX",
				"CGQ-CAN", "PEK-DLC", "XIY-URC", "XMN-PEK", "ZUH-PEK",
				"CAN-TAO", "PEK-CTU", "CAN-CGO", "SZX-HGH", "CAN-XIY",
				"TYN-CAN", "WUH-KMG", "CAN-HRB", "PVG-DLC", "CAN-KWE",
				"SZX-KMG", "TSN-CAN", "PEK-HRB", "CKG-HGH", "DLC-CAN",
				"XIY-CAN", "DLC-PEK", "CTU-SHE", "CAN-CGQ", "SWA-PEK",
				"CSX-PEK", "CGQ-PVG", "WUH-URC", "SHE-CTU", "PEK-CGQ",
				"HRB-PEK", "CAN-TYN", "PEK-SWA", "PEK-ZUH", "TAO-CAN",
				"PEK-XMN", "CGO-CTU", "KMG-SZX", "URC-WUH", "WUH-CTU",
				"CGQ-SZX" };
		for(String str :lines){
			listLine.add(str);
		}
		return listLine;
	}
	
}
